<template>
  <div class="app-container">
    <el-card
      class="filter-container"
      shadow="never"
    >
      <div>
        <i class="el-icon-search"></i>
        <span>筛选搜索</span>
        <el-button
          style="float:right"
          type="primary"
          @click="handleSearchList()"
          size="small"
        >
          查询搜索
        </el-button>
        <el-button
          style="float:right;margin-right: 15px"
          @click="handleResetSearch()"
          size="small"
        >
          重置
        </el-button>
      </div>
      <div style="margin-top: 15px">
        <el-form
          :inline="true"
          :model="listQuery"
          size="small"
          label-width="140px"
        >
          <el-form-item label="标题：">
            <el-input
              v-model="listQuery.title"
              class="input-width"
              placeholder="输入标题"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item label="状态：">
            <el-select
              v-model="listQuery.status"
              placeholder="请选择"
              clearable
              class="input-width"
            >
              <el-option
                v-for="(item,index) in typeOptions"
                :key="index"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
    <el-card
      class="operate-container"
      shadow="never"
    >
      <i class="el-icon-tickets"></i>
      <span>数据列表</span>
      <el-button
        size="mini"
        class="btn-add"
        @click="handleAdd()"
      >新增</el-button>
    </el-card>
    <div class="table-container">
      <el-table
        ref="homeAdvertiseTable"
        :data="list"
        style="width: 100%;"
        @selection-change="handleSelectionChange"
        v-loading="listLoading"
        border
      >
        <el-table-column
          label="标题"
          align="center"
        >
          <template slot-scope="scope">{{ scope.row.title }}</template>
        </el-table-column>
        <el-table-column
          label="状态"
          align="center"
        >
          <template slot-scope="scope">{{ scope.row.status | formatStatus }}</template>
        </el-table-column>
        <el-table-column
          label="生效日期"
          align="center"
        >
          <template slot-scope="scope">{{ scope.row.stime | formatDateTime }}</template>
        </el-table-column>
        <el-table-column
          label="失效日期"
          align="center"
        >
          <template slot-scope="scope">{{ scope.row.etime | formatDateTime }}</template>
        </el-table-column>
        <el-table-column
          label="操作"
          width="300"
          align="center"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              @click="handleAppointmentList(scope.$index, scope.row)"
            >报名信息
            </el-button>
            <el-button
              size="mini"
              type="text"
              @click="handleReview(scope.$index, scope.row)"
            >查看
            </el-button>
            <el-button
              size="mini"
              type="text"
              @click="handleUpdate(scope.$index, scope.row)"
            >编辑
            </el-button>
            <el-button
              size="mini"
              type="text"
              @click="handleIsRecommend(scope.$index, scope.row)"
            >{{ isRecommendOption[scope.row.isRecommend||'0'] }}
            </el-button>
            <el-button
              size="mini"
              type="text"
              @click="handleStatus(scope.$index, scope.row)"
            >{{ statusOption[scope.row.status] }}
            </el-button>
            <el-button
              size="mini"
              type="text"
              @click="handleDelete(scope.$index, scope.row)"
            >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :page-size="listQuery.pageSize"
        :page-sizes="[5,10,15]"
        :current-page.sync="listQuery.pageNum"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>
<script>
import { RcRegistrationContentControllerList, RcRegistrationContentControllerDelete, RcRegistrationContentControllerUpdate } from '@/api/neighbour'

import { formatDate } from '@/utils/date'
const defaultListQuery = {
  classified: '01',
  pageNum: 1,
  pageSize: 10
}
export default {
  name: 'GuideList',
  data () {
    return {
      listQuery: Object.assign({}, defaultListQuery),
      list: null,
      total: null,
      listLoading: false,
      multipleSelection: [],
      loading: false,
      typeOptions: [
        { value: '4', label: '生效' },
        { value: '5', label: '失效' }
      ],
      statusOption: {
        '4': '失效',
        '5': '生效'
      },
      isRecommendOption: {
        '0': '推荐',
        '1': '取消推荐'
      }
    }
  },
  created () {
    this.getList()
  },
  filters: {
    formatDateTime (time) {
      if (time == null || time === '') {
        return 'N/A'
      }
      const date = new Date(time)
      return formatDate(date, 'yyyy-MM-dd hh:mm:ss')
    },
    formatStatus (status) {
      return { '4': '生效', '5': '失效' }[status]
    }
  },
  methods: {
    handleResetSearch () {
      this.listQuery = Object.assign({}, defaultListQuery)
    },
    handleSearchList () {
      this.listQuery.pageNum = 1
      this.getList()
    },
    handleSelectionChange (val) {
      this.multipleSelection = val
    },
    handleSizeChange (val) {
      this.listQuery.pageNum = 1
      this.listQuery.pageSize = val
      this.getList()
    },
    handleCurrentChange (val) {
      this.listQuery.pageNum = val
      this.getList()
    },
    handleDelete (index, row) {
      this.deleteGuide(row.id)
    },
    handleAppointmentList (index, row) {
      this.$router.push({
        path: '/nbh-ms/appointmentActivitiesList',
        query: { id: row.id }
      })
    },
    handleReview (index, row) {
      this.$router.push({
        path: '/nbh-ms/reviewActivitiesDetail',
        query: { id: row.id }
      })
    },
    handleAdd () {
      this.$router.push({ path: '/nbh-ms/addActivities' })
    },
    handleIsRecommend (index, row) {
      this.updateRecommend(row.id, row.isRecommend === '0' || row.isRecommend === null ? '1' : '0')
    },
    handleStatus (index, row) {
      this.updateGuide(row.id, row.status === '4' ? '5' : '4')
    },
    handleUpdate (index, row) {
      this.$router.push({
        path: '/nbh-ms/updateActivities',
        query: { id: row.id }
      })
    },
    getList () {
      this.listLoading = true
      const params = {
        ...this.listQuery
      }
      RcRegistrationContentControllerList(params).then((response) => {
        this.listLoading = false
        this.list = response.data.list
        this.total = response.data.total
      })
    },
    updateGuide (id, status) {
      this.$confirm('是否要提交?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        RcRegistrationContentControllerUpdate({ id, status }).then((response) => {
          this.getList()
        })
      })
    },
    updateRecommend (id, isRecommend) {
      this.$confirm('是否要提交?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        RcRegistrationContentControllerUpdate({ id, isRecommend }).then((response) => {
          this.getList()
        })
      })
    },
    deleteGuide (id) {
      this.$confirm('是否要删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        RcRegistrationContentControllerDelete({ id }).then((response) => {
          this.getList()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
      })
    }
  }
}
</script>
<style scoped>
  .input-width {
    width: 203px;
  }
</style>
